<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>基于Ajax的分页列表</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../function/ajax_func.js"></script>
<script language="javascript">
//页面加载，获取第一页的新闻列表
function winload() {
	send_request("get","../news/newsManager?currentPage=1",null,"XML",populateList);
}
//将所获取的新闻列表填充到页面的相应位置
function populateList() {
	if (http_request.readyState == 4) { // 判断对象状态
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
			var doc = http_request.responseXML;
			var currentPage = doc.getElementsByTagName("currentPage")[0].firstChild.data;
			var totalPage = doc.getElementsByTagName("totalPage")[0].firstChild.data;
			var prePage = parseInt(currentPage) - 1;
			var nextPage = parseInt(currentPage) + 1;
			var newslist = doc.getElementsByTagName("news");
			var innerHTML = "";
			if((newslist!=null)&&(newslist.length!=0)) {
				innerHTML += "<table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\" border=\"0\">\r\n";
				innerHTML += "<tr><td width='10%' height='25'>ID</td><td width='65%'>标题</td><td width='25%'>提交日期</td></tr>";
				for(var i=0;i<newslist.length;i++) {
					var news = newslist[i];
					var id = news.getAttribute("id");
					var title = (news.childNodes[0].firstChild==null)?"":news.childNodes[0].firstChild.data;
					var submittime = (news.childNodes[1].firstChild==null)?"":news.childNodes[1].firstChild.data;
					innerHTML += "<tr>";
					innerHTML += "<td width='10%' height='25'>"+id+"</td>";
					innerHTML += "<td width='65%' height='25'>"+title+"</td>";
					innerHTML += "<td width='25%' height='25'>"+submittime+"</td>";
					innerHTML += "</tr>";
				}
				innerHTML += "</table>\r\n";
			}else {
				innerHTML += "暂时没有任何新闻";
			}
			//window.alert(currentPage+"|"+nextPage+"|"+prePage+"|"+totalPage);
			document.getElementById("newslist").innerHTML = innerHTML;
			document.getElementById("prePage").innerHTML="<a href=\"javascript:void(0)\" onClick=\"goToPage('"+prePage+"')\">上一页</a>";
			document.getElementById("nextPage").innerHTML="<a href=\"javascript:void(0)\" onClick=\"goToPage('"+nextPage+"')\">下一页</a>";
			document.getElementById("lastPage").innerHTML="<a href=\"javascript:void(0)\" onClick=\"goToPage('"+totalPage+"')\">最末页</a>";
			//window.alert(currentPage);
		} else { //页面不正常
			alert("您所请求的页面有异常。");
		}
	}
}
//页面跳转
function goToPage(page) {
	send_request("get","../news/newsManager?currentPage="+page,null,"XML",populateList);
}
</script>
</head>

<body onLoad="winload()">
<table width="500" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td align="center">新闻列表</td>
  </tr>
</table>
<table width="500" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td align="center" height="200" valign="top">
		<label id="newslist"></label>
		</td>
  </tr>
</table>
<table width="500" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td align="center">
		<label id="firstPage"><a href="javascript:void(0)" onClick="goToPage('1')">第一页</a></label>
		<label id="prePage">上一页</label>
		<label id="nextPage">下一页</label>
		<label id="lastPage">最末页</label>
		</td>
  </tr>
</table>
</body>
</html>
